package com.bytz.modules.cms.util;

import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.ReflectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Assertions;

import java.util.ArrayList;
import java.util.Arrays;

@AllArgsConstructor
@Getter
@Setter
public class EntityFieldChecker<E>  extends ArrayList<EntityFieldCheckerItem<E>> {

    @SafeVarargs
    public EntityFieldChecker(EntityFieldCheckerItem<E>... checkers) {
        this.addAll(Arrays.asList(checkers));
    }

    public void doCheck(E datum, String title) {
        for (EntityFieldCheckerItem<E> checker : this) {
            String fieldName = LambdaUtil.getFieldName(checker.getField());
            Object value = checker.getValue();
            Object datumValue = ReflectUtil.getFieldValue(datum, fieldName);

            Assertions.assertEquals(value, datumValue, title + "【 " + fieldName + " 】值不正确");
        }
    }
}
